package cn.iocoder.yudao.module.blog.controller.admin.improvemanager;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.blog.controller.admin.improvemanager.vo.*;
import cn.iocoder.yudao.module.blog.dal.dataobject.improvemanager.BlogImproveManagerDO;
import cn.iocoder.yudao.module.blog.convert.improvemanager.BlogImproveManagerConvert;
import cn.iocoder.yudao.module.blog.service.improvemanager.BlogImproveManagerService;

@Tag(name = "管理后台 - 课程权限")
@RestController
@RequestMapping("/blog/improve-manager")
@Validated
public class BlogImproveManagerController {

    @Resource
    private BlogImproveManagerService improveManagerService;

    @PostMapping("/create")
    @Operation(summary = "创建课程权限")
    @PreAuthorize("@ss.hasPermission('blog:improve-manager:create')")
    public CommonResult<Long> createImproveManager(@Valid @RequestBody BlogImproveManagerCreateReqVO createReqVO) {
        return success(improveManagerService.createImproveManager(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新课程权限")
    @PreAuthorize("@ss.hasPermission('blog:improve-manager:update')")
    public CommonResult<Boolean> updateImproveManager(@Valid @RequestBody BlogImproveManagerUpdateReqVO updateReqVO) {
        improveManagerService.updateImproveManager(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除课程权限")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('blog:improve-manager:delete')")
    public CommonResult<Boolean> deleteImproveManager(@RequestParam("id") Long id) {
        improveManagerService.deleteImproveManager(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得课程权限")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blog:improve-manager:query')")
    public CommonResult<BlogImproveManagerRespVO> getImproveManager(@RequestParam("id") Long id) {
        BlogImproveManagerDO improveManager = improveManagerService.getImproveManager(id);
        return success(BlogImproveManagerConvert.INSTANCE.convert(improveManager));
    }

    @GetMapping("/list")
    @Operation(summary = "获得课程权限列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('blog:improve-manager:query')")
    public CommonResult<List<BlogImproveManagerRespVO>> getImproveManagerList(@RequestParam("ids") Collection<Long> ids) {
        List<BlogImproveManagerDO> list = improveManagerService.getImproveManagerList(ids);
        return success(BlogImproveManagerConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得课程权限分页")
    @PreAuthorize("@ss.hasPermission('blog:improve-manager:query')")
    public CommonResult<PageResult<BlogImproveManagerRespVO>> getImproveManagerPage(@Valid BlogImproveManagerPageReqVO pageVO) {
        PageResult<BlogImproveManagerDO> pageResult = improveManagerService.getImproveManagerPage(pageVO);
        return success(BlogImproveManagerConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出课程权限 Excel")
    @PreAuthorize("@ss.hasPermission('blog:improve-manager:export')")
    @OperateLog(type = EXPORT)
    public void exportImproveManagerExcel(@Valid BlogImproveManagerExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BlogImproveManagerDO> list = improveManagerService.getImproveManagerList(exportReqVO);
        // 导出 Excel
        List<BlogImproveManagerExcelVO> datas = BlogImproveManagerConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "课程权限.xls", "数据", BlogImproveManagerExcelVO.class, datas);
    }

}
